<?php

/**
 * @author Jeremy
 * @author Remi
 * @version 1.0
 * @abstract
 */
abstract class Tools
{

    /**
     * Renvoie les differences entre 2 tableaux a plusieurs dimensions
     * @static
     * @param array $a
     * @param array $b
     * @return array
     */
    static public function array_diff_recursive($a, $b)
    {
        $result = array();
        foreach ($a as $k => $v)
        {
            if (array_key_exists($k, $b))
            {
                if (is_array($v))
                {
                    $result[$k] = Tools::array_diff_recursive($v, $b[$k]);
                }
                else
                {
                    if ($v != $b[$k])
                    {
                        $result[$k] = $v;
                    }
                }
            }
            else
            {
                $result[$k] = $v;
            }
        }
        return $result;
    }

    /**
     * Renvoie l'intersection entre 2 tableaux a plusieurs dimensions
     * @static
     * @param array $a
     * @param array $b
     * @return array
     */
    static public function array_intersect_recursive($a, $b)
    {
        $result = array();
        foreach ($a as $k => $v)
        {
            if (array_key_exists($k, $b))
            {
                if (is_array($v))
                {
                    $result[$k] = Tools::array_intersect_recursive($v, $b[$k]);
                }
                else
                {
                    if ($v == $b[$k])
                    {
                        $result[$k] = $v;
                    }
                }
            }
        }
        return $result;
    }

    /**
     * Convertit une date de format "JJ/MM/YYYY" en Timestamp unix
     * @static
     * @param string $date
     * @return int
     */
    static public function transformDateToTimestamp($date)
    {
        if (isset($date) && !empty($date))
        {
            $date = explode("/", $date);
            $time = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
            return $time;
        }
        return 0;
    }

    
    static public function transformDureeToTimestamp($mois=0, $jours=0)
    {
        $time = 0;
        $time = $time + ($jours*24*60*60);
        $time = $time + ($mois*30*24*60*60);
        return $time;
    }

    
    static public function transformTimestampToDuree($timestamp)
    {
        $mois = floor($timestamp/(30*24*60*60));
        $jours = ($timestamp - ($mois*30*24*60*60))/(24*60*60);
        $time = array();
        $time['mois'] = $mois;
        $time['jours'] = $jours;
        return $time;
    }

    static public function generatePassword($nbCar=6)
    {
        // générer un mot de passe
        // Ensemble des caractères utilisés pour le créer
        $cars = "azerty2ui3p4qs5df6gh7jk8lm9wxcvbn";
        // Combien on en a mis au fait ?
        $wlong = strlen($cars);
        // Au départ, il est vide ce mot de passe ;)
        $wpas = "";
        // Combien on veut de caractères pour ce mot de passe ?
        $taille = $nbCar;
        // On initialise la fonction aléatoire
        srand((double) microtime() * 1000000);
        // On boucle sur le nombre de caractères voulus
        for ($i = 0; $i < $taille; $i++)
        {
            // Tirage aléatoire d'une valeur entre 1 et wlong
            $wpos = rand(0, $wlong - 1);
            // On cumule le caractère dans le mot de passe
            $wpas = $wpas . substr($cars, $wpos, 1);
            // On continue avec le caractère suivant à générer
        }
        // On affiche le mot de passe (on peut le stocker quelque part...)
        return $wpas;
    }

    static public function add_more($post_content)
    {
        $options = array('em_min_chars_to_skip' => 10,
            'em_paras_to_skip' => 1,
            'em_link_on_new_para' => true
        );

        $char_skip_count = intval($options['em_min_chars_to_skip']);
        $para_skip_count = intval($options['em_paras_to_skip']);
        $link_on_new_para = $options['em_link_on_new_para'];

        // Skip a number of initial characters
        $skipped_chars = substr($post_content, 0, $char_skip_count);
        $unskipped_chars = substr($post_content, $char_skip_count);

        // Pattern matching an HTML tag that indicates a paragraph
        $para_tag = '(?:p|pre|blockquote|div|ol|ul|h[1-6]|table)';
        // Pattern matching two consecutive newlines with optional space between
        $double_newline = "(?:\r\n *\r\n|\r *\r|\n *\n|<br\s*/?>\s*<br\s*/?>)";
        // Pattern matching optional whitespace
        $ws = '\s*';
        // Pattern matching paragraph body (must start at the beginning
        // of a paragraph, and be followed by a paragraph end)
        $body = '.+?';
        // Pattern matching the end of a paragraph
        $end = "(?:$double_newline|</$para_tag>|(?<=\W)(?=$ws<$para_tag\W))";

        // Get all the skipped paragraphs, but separate the end of the final paragraph so
        // we can add a "run-on" more if necessary.
        // regex finds: a para body; followed by (n-1) end+body pairs; followed by an end; followed by something.
        $para_skip_dec = $para_skip_count - 1;
        if (preg_match("!^($ws$body(?:$end$ws$body){" . $para_skip_dec . "})($end)$ws\S!is", $unskipped_chars, $matches))
        {
            $skipped_paras = $matches[1];
            $skipped_end = $matches[2];
            $unskipped_paras = substr($unskipped_chars, strlen($skipped_paras) + strlen($skipped_end));
            if ($link_on_new_para)
            {
                // Add 2 newlines after the more, to stop WP adding
                // a <br> after the more which leaves a spurious blank line.
                return strip_tags($skipped_chars . $skipped_paras . $skipped_end) . "<!--" . $unskipped_paras . "-->";
            }
            else
            {
                return strip_tags($skipped_chars . $skipped_paras) . '<!--' . $skipped_end . $unskipped_paras . "-->";
            }
        }

        return strip_tags($post_content);
    }

    static public function parseModelMail($search,$data, $str)
    {
        return str_replace($search, $data, $str);
    }

}

?>